﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using SqlSugar.Extensions;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodfeedServices : Repository<corecmsfoodfeed>, IcorecmsfoodfeedServices
    {
        #region 实现重写增删改查操作==========================================================

        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public bool InsertAsync(corecmsfoodfeed entity)
        {
            return base.Change<corecmsfoodfeed>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodfeed> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodfeed entity)
        {
            return base.Change<corecmsfoodfeed>().Update(a => new corecmsfoodfeed()
            {
                corpId = entity.corpId,
                storeId = entity.storeId,
                feedTypeId = entity.feedTypeId,
                name = entity.name,
                price = entity.price,
                purPrice = entity.purPrice,
                departAsType = entity.departAsType,
                departId = entity.departId,
                helpCode = entity.helpCode,
                pinYin = entity.pinYin,
                warrantyDate = entity.warrantyDate,
                warrantyType = entity.warrantyType,
                code = entity.code,
                isPrint = entity.isPrint,
                changePrice = entity.changePrice,
                discounts = entity.discounts,
                status = entity.status,
                image = entity.image,
                currencyFlag = entity.currencyFlag,
                updateTime = entity.updateTime
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodfeed> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodfeed>().Update(a => new corecmsfoodfeed()
                {
                    corpId = item.corpId,
                    storeId = item.storeId,
                    feedTypeId = item.feedTypeId,
                    name = item.name,
                    price = item.price,
                    purPrice = item.purPrice,
                    departAsType = item.departAsType,
                    departId = item.departId,
                    helpCode = item.helpCode,
                    pinYin = item.pinYin,
                    warrantyDate = item.warrantyDate,
                    warrantyType = item.warrantyType,
                    code = item.code,
                    isPrint = item.isPrint,
                    changePrice = item.changePrice,
                    discounts = item.discounts,
                    status = item.status,
                    image = item.image,
                    currencyFlag = item.currencyFlag,
                    updateTime = item.updateTime
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodfeed>().Update(a => new corecmsfoodfeed()
            {
                isDelete = true,
                updateTime = DateTime.Now
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodfeed>().Update(a => new corecmsfoodfeed()
                {
                    isDelete = true,
                    updateTime = DateTime.Now
                }, a => a.id == id);
            }
            return returnKey;
        }

        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodfeed> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodfeed> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, long typeId, string sName, long storeId = 0)
        {
            List<corecmsfoodfeed> lst = new List<corecmsfoodfeed>();
            var where = Expressionable.Create<corecmsfoodfeed>();
            where = where.And(a => a.isDelete == false);
            if (!string.IsNullOrEmpty(sName))
            {
                where = where.And(a => a.name.Contains(sName) || a.pinYin.Contains(sName));
            }
            if (typeId > 0)
                where = where.And(a => a.feedTypeId == typeId);
            if (storeId > 0)
                where = where.And(a => a.storeId == storeId);
            if (pageIndex >= 1 && pageSize > 0)
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.name).ToList();
            }
            else
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.name).ToList();
            }

            return lst;
        }
        #endregion

        #region 根据点菜单Id获取商品做法
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodfeed> QueryFromOrdering(long orderingId, long foodId, long storeId = 0)
        {
            var foodFeeds = new corecmsfooddetailfeedServices().GetList().Where(p => p.foodId == foodId).ToList();

            List<corecmsfoodfeed> lst = new List<corecmsfoodfeed>();
            var where = Expressionable.Create<corecmsfoodfeed>();
            where = where.And(a => a.isDelete == false);
            if (foodFeeds.Count > 0)
            {
                var taskIds = foodFeeds.Select(p => p.feedId).ToList();
                where = where.And(a => (a.currencyFlag == 1 && a.storeId == storeId) || taskIds.Contains(a.id));
            }
            else
            {
                where = where.And(a => a.currencyFlag == 1 && a.storeId == storeId);
            }

            lst = base.AsQueryable().WhereIF(true, where.ToExpression())
                .LeftJoin<corecmsfoodfeedcategory>((f, s) => f.feedTypeId == s.id)
                .Select((f, s) => new corecmsfoodfeed
                {
                    id = f.id,
                    name = f.name,
                    feedTypeId = f.feedTypeId,
                    feedTypeName = s.name,
                    orderingFeedId = 0,
                    selectedFlag = false,
                    price = f.price,
                }).OrderBy(f => f.feedTypeId).ToList();

            if (orderingId > 0)
            {
                var orderingFeeds = new corecmsfoodorderingfeedServices().GetList().Where(p => p.detailId == orderingId).ToList();

                if (orderingFeeds.Count > 0)
                {
                    foreach (var item in lst)
                    {
                        var orderingFeed = orderingFeeds.Where(p => p.feedId == item.id).FirstOrDefault();
                        if (orderingFeed != null)
                        {
                            item.orderingFeedId = orderingFeed.id;
                            item.selectedFlag = true;
                            item.quantity = Convert.ToInt32(orderingFeed.quantity);
                        }
                    }
                }
            }

            return lst;
        }
        #endregion

        #region 根据点菜单Id获取商品做法
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodfeed> QueryForSelected(List<long> ids)
        {
            var list = base.AsQueryable().Where(p => ids.Contains(p.id)).ToList();
            foreach (var item in list)
            {
                item.selectedFlag = true;
            }
            return list;
        }
        #endregion

    }
}
